package com.zzz.treecomponent.util;

import java.util.*;

public class PrefixTree {
    private final Node root = new Node(null);

    private char replace = '*';

    private List<String> filterString;

    public void setReplace(char replace) {
        this.replace = replace;
    }

    public char getReplace() {
        return this.replace;
    }

    public void init(String... prefixs) {
        if (prefixs.length > 0) {
            filterString = Arrays.asList(prefixs);
        } else{
            filterString = Collections.emptyList();
        }
        initFilter();
    }

    private void initFilter() {
        for (String s : filterString) {
            Node temp = root;
            for (int i = 0; i < s.length(); i++) {
                Node node = temp.nextHasData(s.charAt(i));
                if (temp.next.isEmpty() || node == null) {
                    Node newNode = new Node(s.charAt(i));
                    temp.next.add(newNode);
                    temp = newNode;
                } else {
                    temp = node;
                }
            }
        }
    }

    public void filter(StringBuilder stringBuilder) {
        if (stringBuilder == null || stringBuilder.length() == 0 || root.next.isEmpty()) {
            return;
        }
        int start = 0, end = 0;
        Node temp = root;
        while (end < stringBuilder.length()) {

            temp = temp.nextHasData(stringBuilder.charAt(end));
            if (temp == null) {
                start++;
                end = start;
                temp = root;
            } else {
                end++;
            }
            if (temp.getNext().isEmpty()) {
                change(start, end - 1, stringBuilder, getReplace());
                start = end;
                temp = root;
            }

        }
    }

    public static void main(String[] args) {
        PrefixTree prefixTree = new PrefixTree();
        prefixTree.init("爸", "妈");
        String text = "虎爸猫妈";
        StringBuilder stringBuilder = new StringBuilder(text);
        prefixTree.filter(stringBuilder);
        System.out.println(stringBuilder);

    }

    private void change(int start, int end, StringBuilder builder, char c) {
        if (start > end || end > builder.length() - 1) {
            return;
        }
        for (int i = start; i <= end; i++) {
            builder.setCharAt(i, c);
        }
    }

    private void addNode(Node node, Character data) {
        Node hasData = node.nextHasData(data);
        if (hasData != null) {
            addNode(hasData, data);
        } else {
            node.next.add(new Node(data));
        }
    }
    private static class Node {
        private Character data;
        private final Set<Node> next = new HashSet<>();

        protected Node(Character data) {
            this.data = data;
        }

        public Character getData() {
            return this.data;
        }

        public void setData(Character data) {
            this.data = data;
        }

        public Set<Node> getNext() {
            return next;
        }

        public Node nextHasData(Character data) {
            for (Node node : next) {
                if (node.getData().equals(data)) {
                    return node;
                }
            }
            return null;
        }
    }
}
